﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HotelResSystem2
{
    public class RoomInventory
    {
        //List<Room> roomList = new List<Room>();
        //Room[] roomList2 = new Room[100];
        List<int> roomNums = new List<int>();

        public static readonly string DEFAULT_TYPE = "abc";

        public RoomInventory()
        {
            this.setupRoomList();

        }

        private static int TotalRooms = 0;
        public int totalRooms
        {
            get { return TotalRooms; }
            set { TotalRooms = (int)value; }
        }

        // dummy function for now...
        public bool CheckRoomAvailability(int roomNum)
        {
            for (int i = 0; i < roomNums.Count(); i++)
            {
                if (roomNums[i] == roomNum)
                {
                    return true;
                }
            }
            return false;
        }

        public void AddRoom(Room room)
        {
            // store room num in local list for quick lookup
            int roomNumber = room.RoomNum;
            roomNums.Add(roomNumber);
            totalRooms++;

            // store new room in db
            DataStorage dataStorage = DataStorage.GetInstance();
            //dataStorage.storeNewRoom(room);
        }

        public void RemoveRoom(int roomNum)
        {
            for (int i = 0; i < roomNums.Count; i++)
            {
                if (roomNums[i] == roomNum)
                {
                    roomNums.Remove(roomNum);
                    TotalRooms--;
                }
            }
        }

        // dummy function for now
        public bool holdroom(int roomNum)
        {
            for (int i = 0; i < roomNums.Count(); i++)
            {
                if (roomNums[i] == roomNum)
                {
                    return true;
                }
            }
            return false;
        }


        public void setupRoomList()
        {
            roomNums = DataStorage.GetInstance().getRooms();
            this.totalRooms = roomNums.Count;
        }

        public void checkRoomStatus(int roomNum, DateTime checkInTime, DateTime checkOutTime)
        {
            Room findRoom = getRoom(roomNum);
            DateTime[] roomBooked = findRoom.getBooked();

            bool available = true;

            TimeSpan totalDays = checkOutTime - checkInTime;

            for (int i = 0; i < 20; i++)
            {
                if (roomBooked[i].Ticks > checkInTime.Ticks && roomBooked[i].Ticks < checkOutTime.Ticks)
                {
                    available = false;
                    break;
                }
            }

            this.publishRoomAvailEvent("room.avail", available);

        }

        public bool findAvailableRooms(DateTime checkInTime, DateTime checkOutTime)
        {
            List<int> roomList = DataStorage.GetInstance().getRooms();
            Room findRoom;
            DateTime[] availability;

            foreach (int room in roomList)
            {
                findRoom = getRoom(room);
                availability = findRoom.getBooked();

                bool available = true;
                TimeSpan totalDays = checkOutTime - checkInTime;

                for (int i = 0; i < 20; i++)
                {
                    if (availability[i].Ticks > checkInTime.Ticks && availability[i].Ticks 
                        < checkOutTime.Ticks)
                    {
                        available = false;
                        break;
                    }
                }
                if (!available)
                {
                    roomList.Remove(room);
                }
            }
            this.ShowAvailableRoomEvent(roomList);
            return (roomList.Count > 0);
        }

        List<PropertyListener> propertyListeners = new List<PropertyListener>();

        public void publishRoomAvailEvent(string name, bool value)
        {
            for (int i = 0; i < propertyListeners.Count; i++)
            {
                propertyListeners[i].onRoomAvailEvent(this, name, value);
            }
        }

        public void ShowAvailableRoomEvent(List<int> rooms)
        {
            for (int i = 0; i < propertyListeners.Count; i++)
            {
                propertyListeners[i].onCheckAvailEvent(this, rooms);
            }
        }

        public void addRoomAvailListener(PropertyListener lis)
        {
            propertyListeners.Add(lis);
        }

        public Room getRoom(int roomNum)
        {
            Room tempRoom = new Room();
            DataStorage data = DataStorage.GetInstance();

            for (int i = 0; i < roomNums.Count; i++)
            {
                if (roomNums[i] == roomNum)
                {
                    tempRoom = data.getRoom(roomNum);
                    break;
                }
            }
            return tempRoom;
        }
    }
}
